import { _decorator, Component, Node, tween, Vec3, Animation, PolygonCollider2D, UITransform, Label } from 'cc';
import { Game } from '../Game';
const { ccclass, property } = _decorator;

/** 障碍物的锚点是(0.5,0),因此需要设置坐标=目标点坐标-中心点坐标 */
@ccclass('Block')
export class Block extends Component {
    @property(Label) scoreLabel: Label = null;
    @property(Label) indexLabel: Label = null;

    public arrChildren: Node[];
    public score: number;
    public blockType: any;
    public Points: Vec3[];

    public centerPoint: Node;      // 中心点
    public get pos_dianWorldPos(): Vec3 {
        return this.node.getPosition().add(this.centerPoint.position);
    }
    public index: number;          // 第几个障碍物
    public canTouch: boolean = false;

    onLoad() {
        this.arrChildren = this.node.children;
        this.canTouch = true;
    }

    public Init(pos_dian: Vec3, blockType: number, index: number) {
        this.score = 0;
        this.blockType = blockType;
        this.index = index;

        // 设置中心点
        this.centerPoint = this.arrChildren[blockType].getChildByName('dian');
        this.centerPoint.active = true;    // 测试代码
        this.canTouch = false;

        // 节点坐标=目标点坐标-中心点坐标
        var targetPos = pos_dian.subtract(this.centerPoint.position);
        this.node.setPosition(new Vec3(targetPos.x, targetPos.y + 100))

        // 播放动画:从上往下掉落抖动
        const act_1 = tween(this.node).to(0.2, { position: new Vec3(targetPos.x, targetPos.y - 30, 0) });
        const act_2 = tween(this.node).to(0.05, { position: new Vec3(targetPos.x, targetPos.y + 20, 0) });
        const act_3 = tween(this.node).to(0.05, { position: new Vec3(targetPos.x, targetPos.y - 10, 0) });
        const act_4 = tween(this.node).to(0.03, { position: new Vec3(targetPos.x, targetPos.y, 0) });
        const act_5 = tween(this.node).call(() => {
            Game.instance.moveHeroWithBlocks();
        });

        // 初始障碍物1和2跳过动画
        if (index < 3) {
            this.canTouch = true;
            this.node.setPosition(targetPos);
            if (index == 1) {
                this.score = 1;
            }
        } else {
            tween(this.node).sequence(act_1, act_2, act_3, act_4, act_5).start();
        }
        this.setBlockActive();
        //this.getPoints();

        this.scoreLabel.string = this.score.toString();
        this.indexLabel.string = this.index.toString();
    }

    playAnima() {
        this.arrChildren[this.blockType].getChildByName('block')?.getComponent(Animation)?.play();
    }

    private setBlockActive() {
        var children = this.node.children;
        for (let i = 0; i < children.length; i++) {
            if (!children[i].name.startsWith('block')) {
                continue;
            }

            children[i].active = false;
            if (i == this.blockType) {
                children[i].active = true;
            }
        }
    }

    onTouchBegin(dt: any) {
        this.node.scale = new Vec3(this.node.scale.x, this.node.scale.y - dt / 6, this.node.scale.z);
        if (this.node.scale.y <= 0.8) {
            this.node.scale = new Vec3(this.node.scale.x, 0.8, this.node.scale.z);
        }
        Game.instance.nodeHero.scale = new Vec3(Game.instance.nodeHero.scale.x + dt / 10, this.node.scale.y, Game.instance.nodeHero.scale.z);
    }

    onTouchEnd() {
        Game.instance.nodeHero.scale = new Vec3(1, 1, 1);
        var scale_Y = 1 - this.node.scale.y;
        var act_1 = { scale: new Vec3(1 + scale_Y / 2, 1 + scale_Y / 2, 1) };
        var act_2 = { scale: new Vec3(1 - scale_Y / 5, 1 - scale_Y / 5, 1) };
        var act_3 = { scale: new Vec3(1, 1, 1) };
        tween(this.node)
            .to(scale_Y / 5, act_1)
            .to(scale_Y / 8, act_2)
            .to(scale_Y / 10, act_3)
            .start();
    }
}


